package eng.jTrussSolver.Rod;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

import eng.jTrussSolver.Material.MaterialsHashMap;
import eng.jTrussSolver.Node.NodeHashMap;
import eng.jTrussSolver.Section.SectionHashMap;

@XmlType(name = "Rod", propOrder = { "RodID", "NodeStart","NodeEnd","Name", "RodSection", "beamMaterial" })
@XmlAccessorType(XmlAccessType.FIELD)  
public class RodData {
	@XmlElement
	public int RodID;
	@XmlElement
	public int NodeStart;
	@XmlElement
	public int NodeEnd;
	@XmlElement
	public String Name;
	@XmlElement
	public String RodSection;
	@XmlElement
	public String RodMaterial;
	
	
	
	public RodData ()
	{}
	
	public RodData (int RodID,String Name,int NodeStart,int NodeEnd,String rodSection,String rodMaterial) {
		
		this.RodID = RodID;
		this.Name = Name;
		this.NodeStart = NodeStart;
		this.NodeEnd = NodeEnd;
		this.RodSection = rodSection;
		this.RodMaterial = rodMaterial;
	}
	
	public String getRodDataAsString()
	{
		String str="";
		str += "ID:"+ RodID+"\t";
		str += "Name:"+ Name+"\t";
		str += "N.Start:"+ NodeStart+"\t";
		str += "N.End:"+ NodeEnd+"\t";
		str += "Section:"+ RodSection+"\t";
		str += "Material:"+ RodMaterial;
		return str;
		
	}
	
	
	public double getRodA(SectionHashMap Sections) {
		return Sections.get(this.RodSection).Area;
	}

	public double getRodE(MaterialsHashMap Materials) {
		return  Materials.get(this.RodMaterial).E;
	}

	/**
	 * Calculate Beam cos with horizontal axis
	 * @param Nodes
	 * @return cos
	 */
	public double getRod_l(NodeHashMap Nodes) {
		
		double temp = 0;
		int N1 = this.NodeStart;
		int N2 = this.NodeEnd;
		double X1 = Nodes.get(N1).X;
		double X2 =Nodes.get(N2).X;
		
		temp = (X2-X1)/this.getRodL(Nodes);
		
		return temp;
	}

	public double getRod_m(NodeHashMap Nodes) {

		int N1 = this.NodeStart;
		int N2 = this.NodeEnd;
		double Y1 = Nodes.get(N1).Y;
		double Y2 = Nodes.get(N2).Y;
		return (Y2-Y1)/this.getRodL(Nodes);
	}

	/**
	 * calculates the beam length
	 * @param Nodes
	 * @return
	 */
	public double getRodL(NodeHashMap Nodes) {

		int N1 = this.NodeStart;
		int N2 = this.NodeEnd;
		double X1 = Nodes.get(N1).X;
		double Y1 = Nodes.get(N1).Y;
		double X2 = Nodes.get(N2).X;
		double Y2 = Nodes.get(N2).Y;
		return Math.sqrt( (Math.pow((X2-X1),2))+(Math.pow((Y2-Y1),2)) );
	}

}
